﻿using System;
using System.Collections.Generic;
using System.Linq;
using SaaS.Model.Entity.Sites;
using SaaS.Model.Enums;
using Web.Client.Services.Orders.Dto;
using XCore.Logger;
using XCore.Dapper;
using XCore.Extensions;
using System.Data;
using XCore.Dependency;

namespace Web.Client.Services.Orders
{
    public class OrderAppService : ClientApplicationServiceBase, ISingletonDependency
    {
        /// <summary>
        /// 订单状态检查
        /// </summary>
        private Dictionary<OrderStatus, OrderStatus[]> OrderStautsCheck = new Dictionary<OrderStatus, OrderStatus[]>()
        {
            {OrderStatus.UnPaid,new OrderStatus[]{ OrderStatus.Paying,OrderStatus.CancelPaid }},
            {OrderStatus.Paying,new OrderStatus[]{ OrderStatus.Paid,OrderStatus.CancelPaid }},
            {OrderStatus.CancelPaid,new OrderStatus[] { OrderStatus.Paid} }
        };
        public bool CreateAsync(int userId, int projectDetailId)
        {
            if (!SCC.Tenants.Any(c => c.Id == userId)) throw new MessageException("登录用户不存在");
            var projectDetail = SCC.ProjectDetails.FirstOrDefault(c => c.Id == projectDetailId);
            if (projectDetail == null) throw new MessageException("产品信息不存在");
            var project = SCC.Projects.FirstOrDefault(c => c.Id == projectDetail.ProjectId);
            if (project == null) throw new MessageException("产品信息不存在");
            using (IDapperDatabase dapper = CreateDapperDatabase(IsolationLevel.ReadUncommitted))
            {
                var order = new Order
                {
                    ProjectId = project.Id,
                    ProjectDetailId = projectDetailId,
                    ProjectName = project.Name,
                    Discount = projectDetail.Discount,
                    UserId = userId,
                    Status = OrderStatus.UnPaid,
                    TotalPrice = Math.Round(projectDetail.Price * projectDetail.Discount, 2),
                    Price = projectDetail.Price,
                };
                if (!dapper.Insert(order))
                {
                    dapper.RollBack();
                    throw new MessageException("订单创建失败");
                }
                var operation = new Operation
                {
                    ProjectId = project.Id,
                    Status = OperationStatus.Running,
                    Type = OperationsType.SaaS,
                    UserId = userId,
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now.AddMonths(projectDetail.Month),
                };
                if (!dapper.Insert(operation))
                {
                    dapper.RollBack();
                    throw new MessageException("订单创建失败");
                }
                dapper.Commit();
            }
            return true;
        }
        public OrderDto GetInfo(int orderId)
        {
            return Repository.Get<Order, OrderDto>(orderId);
        }

        public void UpdateStatus(int orderId, OrderStatus status)
        {
            if (!SCC.Orders.Any(c => c.Id == orderId)) throw new MessageException("不存在该订单");
            var value = SCC.Orders.Where(c => c.Id == orderId).Select(c => c.Status).FirstOrDefault();
            if (!OrderStautsCheck.ContainsKey(value)) throw new MessageException("不可修改的状态");
            Repository.Update<Order, OrderStatus>(orderId, c => c.Status, status);
        }
        public bool Delete(int orderId, int userId)
        {
            if (!SCC.Orders.Any(c => c.Id == orderId && c.UserId == userId)) throw new MessageException("不存在该订单");
            return Repository.Update<Order, OrderStatus>(orderId, c => c.Status, OrderStatus.Delete);
        }
    }
}
